package com.dao.impl;

import com.dao.AdminDao;
import com.entity.Users;
import com.util.DBUtil;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.jasypt.util.text.StrongTextEncryptor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {

    private PasswordEncryptor passwordEncryptor;
    private static final String SELECT_USER_SQL = "select userid,username,pwdHash,pwhHashCode,birthday,sex,usertype,isfull,money from users where usertype=3 AND username=?";
    private static final String SELECT_ALLUSER_SQL= "select userid,username,pwdHash,pwhHashCode,birthday,sex,usertype,isfull,money from users";
    private static final String DELETE_USER_SQL= "delete from users where userid=?";

    private static final String SELECT_USERBYID_SQL = "select userid,username,pwdHash,pwhHashCode,birthday,sex,usertype,isfull,money from users where userid=?";
    public AdminDaoImpl(){
        this.passwordEncryptor=new StrongPasswordEncryptor();
    }
    @Override
    public Users login(String username, String password) throws Exception {
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(SELECT_USER_SQL)
        ) {
            Users user =new Users();
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                if(this.passwordEncryptor.checkPassword(password,rs.getString("pwdHash"))){
                    user.setUserid(rs.getLong("userid"));
                    user.setUsername(rs.getString("username"));

                    String decrypt = getDecrypt(rs);
                    user.setPwdHash(decrypt);
                    user.setPwdHash(rs.getString("pwdHash"));
//                    user.getBirthday().format(rs.getString("birthday"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setSex(rs.getString("sex"));
                    user.setUsertype(rs.getLong("usertype"));
                    user.setIsfull(rs.getString("isfull"));
                    user.setMoney(rs.getDouble("money"));
                    return user;
                }else{
                    return null;
                }
            }else {
                return null;
            }
        }
    }



    @Override
    public List<Users> getAllUser() throws Exception {
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(SELECT_ALLUSER_SQL)
        ) {
            List<Users> users = new ArrayList<>();
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                Users user = getUsers(rs);
                users.add(user);
            }
            return users;
        }
    }

    @Override
    public boolean deleteUserById(long id) throws Exception {
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(DELETE_USER_SQL)
        ) {
            ps.setLong(1,id);
            int i = ps.executeUpdate();
            if(i>=1){
                return true;
            }else {
                return false;
            }

        }
    }

    @Override
    public Users selectUserById(int id) throws Exception {
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(SELECT_USERBYID_SQL)
        ) {
            ps.setLong(1,id);
            ResultSet rs = ps.executeQuery();
            Users users = null;
            if(rs.next()) {
                users = getUsers(rs);
            }
            return users;
        }
    }

    private static Users getUsers(ResultSet rs) throws SQLException {
        Users user =new Users();
        user.setUserid(rs.getLong("userid"));
        user.setUsername(rs.getString("username"));
        String decrypt = getDecrypt(rs);
        user.setPwdHash(decrypt);
        user.setBirthday(rs.getDate("birthday"));
        user.setSex(rs.getString("sex"));
        user.setUsertype(rs.getLong("usertype"));
        user.setIsfull(rs.getString("isfull"));
        user.setMoney(rs.getDouble("money"));
        return user;
    }
    private static String getDecrypt(ResultSet rs) throws SQLException {
        String pwdHash = rs.getString("pwdHash");
        String pwdHashCode = rs.getString("pwhHashCode");
        StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
        textEncryptor.setPassword(pwdHash);
        String decrypt = textEncryptor.decrypt(pwdHashCode);
        return decrypt;
    }
}
